package activity.main;

import android.content.Intent;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.laoxinwen.app.R;

import java.util.ArrayList;

import activity.TopicActivity;
import bean.TabTopicItem;
import bean.wraper.TabTopicListWrapper;
import common.BaseActivity;
import common.CommonHolder;
import common.Constants;
import common.FastAdapter;
import common.HttpCallBack;
import common.Requester;
import util.bitmap.Img;

/**
 * Created by ilioili on 2015/3/17.
 */
public class FrgTopicList extends BaseFrgList {

    private ArrayList<TabTopicItem> dataList = new ArrayList<>();

    protected FastAdapter listAdapter = new FastAdapter() {
        @Override
        public int getCount() {
            return dataList.size();
        }

        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
            TabTopicItem tabTopicItem = dataList.get(position);
            BaseActivity activity = (BaseActivity)getActivity();
            CommonHolder.TopicHolder holder = null;
            if (null == convertView) {
                convertView = getActivity().getLayoutInflater().inflate(R.layout.item_topic_origin, parent, false);
                holder = new CommonHolder.TopicHolder(convertView, activity);
            } else {
                holder = (CommonHolder.TopicHolder) convertView.getTag();
            }
            holder.setData(tabTopicItem);
            return convertView;
        }

    };

    @Override
    protected String pageName() {
        return "专题列表页";
    }

    @Override
    protected void requestData(final boolean isFresh) {
        Requester.getDigTabNewsList(bundle.getInt("tab"), pageIndex, new HttpCallBack<TabTopicListWrapper>() {
            @Override
            public void onSucceed(TabTopicListWrapper newsGroupWraper) {
                if (null != newsGroupWraper.getData()) {
                    if (pageIndex == 0) {
                        dataList.clear();
                    }
                    dataList.addAll(newsGroupWraper.getData());
                    if (pageIndex == 0) {
                        listView.setAdapter(listAdapter);
                    } else {
                        listAdapter.notifyDataSetChanged();
                    }
                }
                cancelFreshing();
                if (isFresh) {
                    lastFreshed = getCurrenTimeFlag();
                    proAbsListView.setLastFreshed(lastFreshed);
                }
            }

            @Override
            protected void onNetError() {
                super.onNetError();
                cancelFreshing();
            }

            @Override
            protected void onServerError(TabTopicListWrapper data) {
                super.onServerError(data);
                cancelFreshing();
                //                Toast.makeText(getActivity(), data.getError().getMessage(), Toast.LENGTH_SHORT).show();
            }

            private void cancelFreshing() {
                proAbsListView.cancelFreshing();
                proAbsListView.cancelLoading();
            }

        });



    }
}
